﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Phoenix.Xna.Framework.Components
{
    public sealed class QuadRenderer : DrawableComponent
    {
        private VertexDeclaration _vertexDeclaration = null;
        private VertexPositionTexture[] _vertexBuffer = null;
        private short[] _indexBuffer = null;

        public QuadRenderer()
            : base()
        {
            Initialize(null);
        }

        public QuadRenderer(string id)
            : base(id)
        {
            Initialize(null);
        }

        public QuadRenderer(VertexPositionTexture[] vertexBuffer)
            : base()
        {
            Initialize(vertexBuffer);
        }

        public QuadRenderer(string id, VertexPositionTexture[] vertexBuffer)
            : base(id)
        {
            Initialize(vertexBuffer);
        }

        protected override void Dispose(bool disposing)
        {
            if (!IsDisposed && disposing)
            {
                _vertexBuffer = null;
                _vertexDeclaration.Dispose();
                _indexBuffer = null;
            }
        }

        private void Initialize(VertexPositionTexture[] vertexBuffer)
        {
            _vertexBuffer = vertexBuffer;
        }

        #region DrawableComponent Members

        public override void Draw(GameTime gameTime)
        {
            if (Visible)
            {
                Engine.Instance.GraphicsDevice.VertexDeclaration = _vertexDeclaration;
                Engine.Instance.GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionTexture>(PrimitiveType.TriangleList, _vertexBuffer, 0, 4, _indexBuffer, 0, 2);
            }
        }

        public override void LoadContent()
        {
            if (!IsContentLoaded)
            {
                _vertexDeclaration = new VertexDeclaration(Engine.Instance.GraphicsDevice, VertexPositionTexture.VertexElements);

                if (_vertexBuffer == null)
                {
                    _vertexBuffer = new VertexPositionTexture[]
                    {
                        new VertexPositionTexture(Vector3.Zero, Vector2.One),
                        new VertexPositionTexture(Vector3.Zero, new Vector2(0, 1)),
                        new VertexPositionTexture(Vector3.Zero, Vector2.Zero),
                        new VertexPositionTexture(Vector3.Zero, new Vector2(1, 0))
                    };

                    Vector2 vector1 = Vector2.One * -1;
                    _vertexBuffer[0].Position.X = Vector2.One.X;
                    _vertexBuffer[0].Position.Y = vector1.Y;
                    _vertexBuffer[1].Position.X = vector1.X;
                    _vertexBuffer[1].Position.Y = vector1.Y;
                    _vertexBuffer[2].Position.X = vector1.X;
                    _vertexBuffer[2].Position.Y = Vector2.One.Y;
                    _vertexBuffer[3].Position.X = Vector2.One.X;
                    _vertexBuffer[3].Position.Y = Vector2.One.Y;
                }

                _indexBuffer = new short[] { 0, 1, 2, 2, 3, 0 };
                IsContentLoaded = true;
            }
        }

        public override void UnloadContent()
        {
            if (IsContentLoaded)
            {
                if (_vertexDeclaration != null)
                {
                    _vertexDeclaration.Dispose();
                    _vertexDeclaration = null;
                }

                IsContentLoaded = false;
            }
        }

        #endregion
    }
}